package com.ifinc.common.socket.simple_netty_server;

/**
 * Created by IntelliJ IDEA.
 * User: luotao
 * Date: 12-5-30
 * Time: 00:47
 */

import com.ifinc.common.socket.server.NettyServer;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息处理类
 */

public class SimpleMessageHandler extends SimpleChannelHandler {

    private NettyServer nettyServer;



    @Autowired
    public void setNettyServer(NettyServer nettyServer) {
        this.nettyServer = nettyServer;
    }



    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
            throws Exception {
        System.err.println("e.getMessage():" + e.getMessage());
        ChannelBuffer buf = (ChannelBuffer) e.getMessage();

        ChannelBuffer channelBuffer = ChannelBuffers.buffer(100);
        channelBuffer.writeBytes("OK".getBytes());
        e.getChannel().write(channelBuffer);
        e.getChannel().close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
            throws Exception {

//        super.exceptionCaught(ctx, e);
        System.out.println(e.getCause());

    }

}
